package com.ruoyi.gxs.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.gxs.domain.*;
import com.ruoyi.gxs.mapper.GxsAwardtypeMapper;
import com.ruoyi.gxs.mapper.GxsComprehensiveMapper;
import com.ruoyi.gxs.mapper.GxsYearnameMapper;
import com.ruoyi.gxs.service.*;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsPendAwardMapper;

/**
 * 待审批奖励Service业务层处理
 * 
 * @author 高晓松
 * @date 2023-12-27
 */
@Service
public class GxsPendAwardServiceImpl implements IGxsPendAwardService 
{
    @Autowired
    private GxsPendAwardMapper gxsPendAwardMapper;

    @Autowired
    private GxsAwardtypeMapper gxsAwardtypeMapper;

    @Autowired
    private IGxsStudyChangeService gxsStudyChangeService;

    @Autowired
    private IGxsMoralityService gxsMoralityService;

    @Autowired
    private IGxsSportsService gxsSportsService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private GxsComprehensiveMapper comprehensiveMapper;

    @Autowired
    private GxsYearnameMapper gxsYearnameMapper;

    /**
     * 查询待审批奖励
     * 
     * @param gxsPendAwardid 待审批奖励主键
     * @return 待审批奖励
     */
    @Override
    public GxsPendAward selectGxsPendAwardByGxsPendAwardid(Long gxsPendAwardid)
    {
        return gxsPendAwardMapper.selectGxsPendAwardByGxsPendAwardid(gxsPendAwardid);
    }

    /**
     * 查询待审批奖励列表
     * 
     * @param gxsPendAward 待审批奖励
     * @return 待审批奖励
     */
    @Override
    public List<GxsPendAward> selectGxsPendAwardList(GxsPendAward gxsPendAward)
    {
        return gxsPendAwardMapper.selectGxsPendAwardList(gxsPendAward);
    }

    /**
     * 新增待审批奖励
     * 
     * @param gxsPendAward 待审批奖励
     * @return 结果
     */
    @Override
    public int insertGxsPendAward(GxsPendAward gxsPendAward)
    {
        return gxsPendAwardMapper.insertGxsPendAward(gxsPendAward);
    }

    /**
     * 修改待审批奖励
     * 
     * @param gxsPendAward 待审批奖励
     * @return 结果
     */
    @Override
    public int updateGxsPendAward(GxsPendAward gxsPendAward)
    {
        return gxsPendAwardMapper.updateGxsPendAward(gxsPendAward);
    }

    /**
     * 批量删除待审批奖励
     * 
     * @param gxsPendAwardids 需要删除的待审批奖励主键
     * @return 结果
     */
    @Override
    public int deleteGxsPendAwardByGxsPendAwardids(Long[] gxsPendAwardids)
    {
        return gxsPendAwardMapper.deleteGxsPendAwardByGxsPendAwardids(gxsPendAwardids);
    }

    /**
     * 删除待审批奖励信息
     * 
     * @param gxsPendAwardid 待审批奖励主键
     * @return 结果
     */
    @Override
    public int deleteGxsPendAwardByGxsPendAwardid(Long gxsPendAwardid)
    {
        return gxsPendAwardMapper.deleteGxsPendAwardByGxsPendAwardid(gxsPendAwardid);
    }

    /**
     * 申请奖励
     *
     * @param gxsPendAwardid 待审批奖励主键
     * @return 结果
     */
    @Override
    public int selectApplyAwardid(Long gxsPendAwardid) {
        GxsPendAward gxsPendAward = gxsPendAwardMapper.selectGxsPendAwardByGxsPendAwardid(gxsPendAwardid);
        gxsPendAward.setGxsPendStatus(1L);
        return gxsPendAwardMapper.updateGxsPendAward(gxsPendAward);
    }

    /**
     * 审批奖励
     *
     * @param gxsPendAward 待审批奖励主键
     * @return 结果
     */
    @Override
    public int selectExamineAward(GxsPendAward gxsPendAward) {
        //查看综测类型
        Long gxsTypeId = gxsPendAward.getGxsTypeId();

        Long gxsAwardForm = gxsPendAward.getGxsAwardForm();
        Long gxsWardlevel = gxsPendAward.getGxsWardlevel();
        Long gxsGrade = gxsPendAward.getGxsGrade();

        GxsAwardtype awardtype = new GxsAwardtype();
        awardtype.setGxsAwardForm(gxsAwardForm);
        awardtype.setGxsWardlevel(gxsWardlevel);
        awardtype.setGxsGrade(gxsGrade);

        //根据获奖形式，获奖级别，获奖级别查询获奖具体类别
        GxsAwardtype gxsAwardtype = gxsAwardtypeMapper.selectGxsScoringMechanism(awardtype);
        Long mechanism = gxsAwardtype.getGxsScoringMechanism();


        //专业成绩
        if(gxsTypeId == 0L) {

            //根据学号查询 昵称、用户ID、部门 并添加到gxsStudyChange
            SysUser sysUser = sysUserMapper.selectUserByUserName(gxsPendAward.getUserName());

            GxsStudyChange studyChange = new GxsStudyChange();

            studyChange.setNickName(sysUser.getNickName());
            studyChange.setUserId(sysUser.getUserId());
            studyChange.setDeptId(sysUser.getDeptId());
            studyChange.setUserName(sysUser.getUserName());

            //根据新增的专业分数信息 查询综合得分学习成绩
            GxsComprehensive com = new GxsComprehensive();
            com.setUserName(studyChange.getUserName());
            com.setGxsYear(studyChange.getGxsYear());
            List<GxsComprehensive> gxsComprehensives = comprehensiveMapper.selectGxsComprehensiveList(com);
            GxsComprehensive comprehensive = gxsComprehensives.get(0);

            GxsYearname gxsYearname = gxsPendAward.getGxsYearname();
            studyChange.setGxsYear(gxsYearname.getGxsYearid());

            studyChange.setGxsSchoolrecord(comprehensive.getGxsSchoolrecord());
            studyChange.setGxsBonusState(0L);
            studyChange.setGxsScore(mechanism);

            studyChange.setGxsAddCause("奖励得分" + mechanism);

            studyChange.setGxsTotalPoints(comprehensive.getGxsSchoolrecord() + mechanism);

            gxsStudyChangeService.insertGxsStudyChange(studyChange);
        }

        //思想品德
        if(gxsTypeId == 1L) {
            GxsMorality morality = new GxsMorality();

            //根据学号查询 昵称、用户ID、部门 并添加到gxsStudyChange
            SysUser sysUser = sysUserMapper.selectUserByUserName(gxsPendAward.getUserName());

            morality.setNickName(sysUser.getNickName());
            morality.setUserId(sysUser.getUserId());
            morality.setDeptId(sysUser.getDeptId());
            morality.setUserName(sysUser.getUserName());

            //根据新增的专业分数信息 查询综合得分学习成绩
            GxsComprehensive com = new GxsComprehensive();
            com.setUserName(morality.getUserName());
            com.setGxsYear(morality.getGxsYear());
            List<GxsComprehensive> gxsComprehensives = comprehensiveMapper.selectGxsComprehensiveList(com);
            GxsComprehensive comprehensive = gxsComprehensives.get(0);


            GxsYearname gxsYearname = gxsPendAward.getGxsYearname();
            morality.setGxsYear(gxsYearname.getGxsYearid());

            morality.setGxsMoralityRecord(comprehensive.getGxsThought());
            morality.setGxsBonusState(0L);
            morality.setGxsScore(mechanism);

            morality.setGxsAddCause("奖励得分" + mechanism);

            morality.setGxsTotalPoints(comprehensive.getGxsSchoolrecord() + mechanism);

            gxsMoralityService.insertGxsMorality(morality);

        }

        //体美劳
        if(gxsTypeId == 2L) {
            GxsSports sports = new GxsSports();

            //根据学号查询 昵称、用户ID、部门 并添加到gxsStudyChange
            SysUser sysUser = sysUserMapper.selectUserByUserName(gxsPendAward.getUserName());

            sports.setNickName(sysUser.getNickName());
            sports.setUserId(sysUser.getUserId());
            sports.setDeptId(sysUser.getDeptId());
            sports.setUserName(sysUser.getUserName());

            //根据新增的专业分数信息 查询综合得分学习成绩
            GxsComprehensive com = new GxsComprehensive();
            com.setUserName(sports.getUserName());
            com.setGxsYear(sports.getGxsYear());
            List<GxsComprehensive> gxsComprehensives = comprehensiveMapper.selectGxsComprehensiveList(com);
            GxsComprehensive comprehensive = gxsComprehensives.get(0);

            GxsYearname gxsYearname = gxsPendAward.getGxsYearname();
            sports.setGxsYear(gxsYearname.getGxsYearid());

            sports.setGxsScore(comprehensive.getGxsThought());
            sports.setGxsBonusState(0L);
            sports.setGxsScore(mechanism);

            sports.setGxsAddCause("奖励得分" + mechanism);

            sports.setGxsTotalPoints(comprehensive.getGxsSchoolrecord() + mechanism);

            gxsSportsService.insertGxsSports(sports);
        }

        gxsPendAward.setGxsPendStatus(2L);
        return gxsPendAwardMapper.updateGxsPendAward(gxsPendAward);
    }
}
